package khleuven.mondzorg.app;

import khleuven.mondzorg.Logger;
import khleuven.mondzorg.R;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class FaqsActivity extends ActivityWithIllustrations {
	
	private AutoCompleteTextView actv;
	
	private String faqString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_faqs);
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		initAutoCompleteTextView();
	}
	
	private void initAutoCompleteTextView() {
		actv = (AutoCompleteTextView)findViewById(R.id.faq_search);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.select_dialog_item, dentalcare.getPreferredFaqsAsStringList());
        actv.setThreshold(1);
        actv.setAdapter(adapter);	
        
        actv.setOnItemClickListener(new OnItemClickListener() { 
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            	faqString = arg0.getItemAtPosition(arg2).toString();
            	Logger.logApp("keuze: "+faqString);
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(actv.getWindowToken(), 0);
                setExplanation();        
                setIllustration();
            }
        });
	}	
	
	private void setExplanation(){
        String explanation = dentalcare.getFaqExplanation(faqString);                
        TextView tvexp = (TextView)findViewById(R.id.txt_expl);
        setTextViewHTML(tvexp, explanation);
        tvexp.setMovementMethod(LinkMovementMethod.getInstance());
	}
	
	protected void setIllustration(){
		illustrations = dentalcare.getFaqIllustrations(faqString);  
		super.setIllustration();
	}
	
	protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)	{
	    ClickableSpan clickable = new ClickableSpan() {
	          public void onClick(View view) {	        	  
	              String defString = span.getURL();
	              Logger.logApp("NU MOETEN WE NAAR DEF: "+defString);
	      		  Intent intent = new Intent(FaqsActivity.this, DefinitionsActivity.class);
	      		  intent.putExtra(DefinitionsActivity.DEF_KEY, defString);
	      		  startActivity(intent);	
	          }
	    };
	    super.makeLinkClickable(strBuilder, span, clickable);
	}
}